//
//  Dictionary+URL.swift
//  SomePasti
//
//  Created by 李永彬 on 2025/10/22.
//

import Foundation

extension Dictionary where Key == String, Value == Any {
    /// 将字典转换为URL查询参数字符串
    var urlQueryString: String {
        let queryItems = self.compactMap { key, value -> String? in
            guard let encodedKey = key.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
                  let stringValue = String(describing: value)
                    .addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
                return nil
            }
            return "\(encodedKey)=\(stringValue)"
        }
        
        return queryItems.sorted().joined(separator: "&")
    }
}
